import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/list_ledger_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/hidden_danger_control_page/hidden_danger_control_controller.dart';

/// 隐患治理台账
class HiddenDangerControlPage extends StatelessWidget {
  HiddenDangerControlPage({super.key}){
    Get.lazyPut(HiddenDangerControlController.new);
  }
  @override
  Widget build(BuildContext context) {
    final HiddenDangerControlController controller = Get.find<HiddenDangerControlController>();
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 14.w),
              child: Column(
                children: [
                  _topWidget(controller),
                  AppGaps.vGap14,
                  Container(
                    padding: EdgeInsets.only(left: 10.w, right: 10.w, top: 14.w, bottom: 20.w),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            _trouble(const Color(0xFF5C6DFF), '未处理隐患', '${controller.getCountEntity?.pendingCount}'),
                          ],
                        ),
                        AppGaps.vGap10,
                        Row(
                          children: [
                            _trouble(const Color(0xFF00CE78), '当日处理隐患', '${controller.getCountEntity?.todayCount}'),
                          ],
                        ),
                        AppGaps.vGap10,
                        Row(
                          children: [
                            _trouble(const Color(0xFFFC9900), '逾期隐患', '${controller.getCountEntity?.overdueCount}'),
                          ],
                        ),
                      ],
                    ),
                  ),
                  AppGaps.vGap14,
                  Expanded(
                    child: CustomEasyRefreshList<ListLedgerRecords>(
                      key: controller.globalKey,
                      dataLoad: controller.getRiskSecurityIdentificationList,
                      itemBuilder: (item, index) {
                        return CustomTitleChartPage(
                          title: '${item.dangerName}',
                          child: Column(
                            children: [
                              _title('重大隐患：', item.dangerLevel ?? '空'),
                              _title('风险分析对象：', item.riskObjectName ?? '空'),
                              _title('风险分析单元：', item.riskUnitName ?? '空'),
                              _title('风险事件：', item.riskEventName ?? '空'),
                              _title('管控措施：', item.riskMeasureDesc ?? '空'),
                            ],
                          ),
                        ).click(() {
                          controller.onHiddenDangerDetailsButtonPressed(item.id!);
                        });
                      },
                    ),
                  )
                ],
              ),
            ), // 右侧筛选栏
            // endDrawer: CustomEndDrawer(
            //   centerContext: Column(
            //     children: [
            //       RegionalOrganizationTreePage(
            //         selectedEntity: controller.selectedEntity,
            //         onCallBack: (RiskTreeEntity entity) {
            //           controller.selectedEntity = entity;
            //         },
            //       ),
            //     ],
            //   ),
            //   reset: controller.reset,
            //   confirm: controller.confirm,
            // ),
          );
        });
  }

  /// 标题
  Widget _title(String title, String? value) {
    return Container(
      margin: EdgeInsets.only(top: 7.w, bottom: 7.w),
      child: Row(
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 13.sp,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA5A6B5),
            ),
          ),
          Expanded(
            child: Text(
              value ?? '空',
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xff495061),
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _trouble(Color color, String title, String? value) {
    return Container(
      padding: EdgeInsets.only(left: 10.w, right: 10.w, top: 4.w, bottom: 4.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6),
        color: const Color(0x1A5C6DFF),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            value.toString(),
            style: TextStyle(
              color: color,
              fontSize: 14.sp,
            ),
          ),
          Text(
            title,
            style: TextStyle(
              color: AppColors.font_color_blue,
              fontSize: 12.sp,
            ),
          )
        ],
      ),
    );
  }

  /// 标题
  Widget _getBox(HiddenDangerControlController controller) {
    return Container(
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(5)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: Text(
              '${controller.title}',
              style: TextStyle(
                color: const Color(
                  0xFF495061,
                ),
                fontWeight: FontWeight.w500,
                fontSize: 12.sp,
              ),
            ),
          ),
          Row(
            children: [
              _divider(),
              AppGaps.hGap8,
              Container(
                width: 38.w,
                height: 38.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
                child: SvgPicture.asset(
                  AppImages.icon_search_screen,
                  width: 16.w,
                ),
              ).click(controller.onFilterButtonPressed),
            ],
          ),
        ],
      ),
    ).click(controller.onFilterButtonPressed);
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 40.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0x00649FFF),
        Color(0xFF649FFF),
        Color(0x00649FFF),
      ])),
    );
  }

  Widget _topWidget(HiddenDangerControlController controller) {
    return Row(
      children: [
        Expanded(child: _getBox(controller)),
      ],
    );
  }

}
